package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ProductBean;
import com.project.bean.UserBean;
import com.project.service.IProductService;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IProductService productService;

    @RequestMapping("login")
    public String login(String userName, String pwd,
                        HttpServletRequest req){
        UserBean user = userService.login(userName,pwd);
        if(user == null){
            req.setAttribute("errorInfo","登陆失败");
            return "/login.html";
        }
        //登陆成功，在session绑定登陆信息
        req.getSession().setAttribute("loginUser",user);
        IPage ip = productService.findByItem(1,null);
        req.setAttribute("pg",ip);
        return "/index.html";
    }

    @RequestMapping("findByItem")
    public String findByItem(HttpServletRequest req,String name,
                             @RequestParam(value = "pageNO",defaultValue = "1") Integer pageNO){
        IPage<ProductBean> ip = productService.findByItem(pageNO,name);
        req.setAttribute("pg",ip);
        return "/index.html";
    }
}
